import es
import playerlib

from wcs.libs.players import *
from wcs.libs.messages import *
from wcs.libs.races import *
from wcs.libs.config import *

def hostage_rescued(event_var):
    """
    Executed when a hostage has been resuced, add experience to the player who
    rescued them.

    @param es.EventInfo event_var An automatically passed event instance
    """
    userid = event_var['userid']
    player = players[userid]
    race   = player[player.currentRace]
    amount = int(hostageXP)
    reason = messages(userid, 'hostage xp')
    race.addXP(amount * race.level, reason)

def bomb_defused(event_var):
    """
    Executed when a bomb has been defused, add experience to the player who
    defused it.

    @param es.EventInfo event_var An automatically passed event instance
    """
    userid = event_var['userid']
    player = players[userid]
    race   = player[player.currentRace]
    amount = int(bombDefuseXP)
    reason = messages(userid, 'defuse xp')
    race.addXP(amount * race.level, reason)

def bomb_planted(event_var):
    """
    Executed when a bomb has been planted, add experience to the player who
    planted it.

    @param es.EventInfo event_var An automatically passed event instance
    """
    userid = event_var['userid']
    player = players[userid]
    race   = player[player.currentRace]
    amount = int(bombPlantXP)
    reason = messages(userid, 'plant xp')
    race.addXP(amount * race.level, reason)

def bomb_exploded(event_var):
    """
    Executed when a bomb has exploded, add experience to all the terrorists
    who are still alive who aided in the process of allowing the bomb to
    explode.

    @param es.EventInfo event_var An automatically passed event instance
    """
    for userid in playerlib.getUseridList('#t,#alive'):
        player = players[userid]
        race   = player[player.currentRace]
        amount = int(bombExplodedXP)
        reason = messages(userid, 'explode xp')
        race.addXP(amount * race.level, reason)

def player_jump(event_var):
    """
    Executed when a player jumps; see if they have a long jump modification, if
    so, ensure that the velocity is reassigned.

    @param es.EventInfo event_var An automatically passed event instance
    """
    userid = event_var['userid']
    jumpMultiplier = players[userid].command["jumpMultiplier"]
    if jumpMultiplier > 1.0:
        """ Acqure their current vectors """
        prop = "CBasePlayer.localdata.m_vecVelocity[%s]"
        horizontalVector = es.getplayerprop(userid, prop % "0")
        verticalVector   = es.getplayerprop(userid, prop % "1")

        """ Apply the new force of the multiplier """
        horizontalVector = (jumpMultiplier * horizontalVector) * 0.25
        verticalVector   = (jumpMultiplier * verticalVector)   * 0.25

        """ Create a compliant vector string """
        vector = "%s,%s,0" % (horizontalVector, verticalVector)

        """ Assign the new vector to the players velocity """
        prop = "CBasePlayer.localdata.m_vecBaseVelocity"
        es.setplayerprop(userid, prop, vector)